package com.hghivln.blog.utils;

import com.hghivln.blog.entity.SysUser;
import com.hghivln.blog.shiro.LoginUser;
import com.hghivln.blog.shiro.TokenWebSessionManager;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;

/**
 * Shiro相关工具类  获取当前登录的用户
 * 切换身份 ， 动态修改subject的用户属性
 */
public class ShiroUtils {

    public static LoginUser getLoginUser() {
        Session session = SecurityUtils.getSubject().getSession();
        SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
        if (principalCollection == null) {
            return null;
        }
        return (LoginUser) principalCollection.getPrimaryPrincipal();
    }
}
